import { getBannerAll } from '../../api/banner'
import { getHouseResFilterPage } from '../../api/houseRes'
import { getAreaAll } from '../../api/contracts'
import { checkLogin } from '../../utils/loginStatus'
import { removeToken, removeUserInfo } from '../../utils/storage'
import config from '../../config/index'
Page({
  data: {
    banner_list: [],
    tools: [
      { id: 1, title: '整租', icon: '../../static/images/zhengzhu.png', bg: '#ffc158', url: '/pages/modePage/modePage?mode=FULL' },
      { id: 2, title: '合租', icon: '../../static/images/hezhu.png', bg: '#828cde', url: '/pages/modePage/modePage?mode=MORE' },
      { id: 3, title: '短租', icon: '../../static/images/duanzhu.png', bg: '#42c1a4', url: '/pages/modePage/modePage?mode=SHORT' },
      { id: 4, title: '委托', icon: '../../static/images/weituo.png', bg: '#53c9d3', url: '/pages/contracts/contracts' },
    ],
    config,
    page: 1,
    house_peace: '',
    house_use: '',
    min_rent: '',
    max_rent: '',
    house_type: '',
    rent_label: '',
    resList: [] as any[],
    peace: [
      { text: '区域', value: '' }
    ],
    use: [
      { text: '类型', value: '' },
      { text: '小区', value: '小区' },
      { text: '公寓', value: '公寓' },
      { text: '别墅', value: '别墅' },
      { text: '自建房', value: '自建房' }
    ],
    rent: [
      { text: '租金', value: '' },
      { text: '500以下', value: '0-500' },
      { text: '500-1000', value: '500-1000' },
      { text: '1000-1500', value: '1000-1500' },
      { text: '1500-2000', value: '1500-2000' },
      { text: '2000-2500', value: '2000-2500' },
      { text: '2500-3000', value: '2500-3000' },
      { text: '3000-3500', value: '3000-3500' },
      { text: '3500-4000', value: '3500-4000' },
      { text: '4000-4500', value: '4000-4500' },
      { text: '4500-5000', value: '4500-5000' },
      { text: '5000以上', value: '5000-100000' }
    ],
    type: [
      { text: '户型', value: '' },
      { text: '1室', value: '1' },
      { text: '2室', value: '2' },
      { text: '3室', value: '3' },
      { text: '4室', value: '4' },
      { text: '5室', value: '5' },
      { text: '6室', value: '6' },
      { text: '7室', value: '7' },
      { text: '8室', value: '8' },
      { text: '9室', value: '9' },
      { text: '10室', value: '10' },
    ],
    sort: [
      { text: '排序', value: 0 },
      { text: '价格↑', value: 1 },
      { text: '价格↓', value: 2 },
      { text: '面积↑', value: 3 },
      { text: '面积↓', value: 4 },
    ],
    sort_value: 0
  },
  prviewMedia(e: WechatMiniprogram.TouchEvent) {
    let id = e.currentTarget.dataset.id
    wx.previewMedia({
      current: id,
      sources: this.data.banner_list.map((item: Banner) => {
        return {
          url: config.BaseUrl + '/' + item.banner_url
        }
      }) as WechatMiniprogram.MediaSource[]
    })
  },
  onSortChange(value: any) {
    this.setData({
      sort_value: value.detail,
      resList: [],
      page: 1
    })
    this.initList()
  },
  onPeaceChange(value: any) {
    this.setData({ house_peace: value.detail, page: 1, resList: [] })
    this.initList()
  },
  onUseChange(value: any) {
    this.setData({ house_use: value.detail, page: 1, resList: [] })
    this.initList()
  },
  onRentChange(value: any) {
    let rent = value.detail.split('-')
    if (rent[0] === '') {
      this.setData({
        min_rent: rent[0],
        max_rent: rent[0],
        page: 1,
        resList: []
      })
      this.initList()
      return
    }
    this.setData({
      min_rent: rent[0],
      max_rent: rent[1],
      page: 1,
      resList: []
    })
    this.initList()
  },
  onTypeChange(value: any) {
    this.setData({ house_type: value.detail, page: 1, resList: [] })
    this.initList()
  },
  toPath(e: WechatMiniprogram.TouchEvent) {
    let url = e.currentTarget.dataset.url
    wx.navigateTo({
      url
    })
  },
  async initData() {
    const res = await getBannerAll()
    if (res.statusCode === config.SUCCESS_CODE) {
      this.setData({
        banner_list: res.data
      })
    }
    const areaRes = await getAreaAll()
    if (areaRes.statusCode === config.SUCCESS_CODE) {
      let areas = areaRes.data.map((item: any) => {
        return {
          text: item.item_label,
          value: item.item_value
        }
      })
      this.setData({
        peace: [
          ...this.data.peace,
          ...areas
        ]
      })
    }
  },
  async initList() {
    const pageRes = await getHouseResFilterPage(this.data.page, this.data.house_peace, this.data.house_use, this.data.min_rent, this.data.max_rent, this.data.house_type)
    if (pageRes.statusCode === config.SUCCESS_CODE) {
      if (pageRes.data.data.length === 0) return
      let resList = [
        ...this.data.resList,
        ...pageRes.data.data.filter((item: ListHouseRes) => item.house_status === true)
      ]
      let sort_list: ListHouseRes[]
      if (this.data.sort_value === 1) {
        sort_list = resList.sort((a: ListHouseRes, b: ListHouseRes) => a.house_rent - b.house_rent)
      } else if (this.data.sort_value === 2) {
        sort_list = resList.sort((a: ListHouseRes, b: ListHouseRes) => b.house_rent - a.house_rent)
      } else if (this.data.sort_value === 3) {
        sort_list = resList.sort((a: ListHouseRes, b: ListHouseRes) => Number.parseInt(a.house_area) - Number.parseInt(b.house_area))
      } else if (this.data.sort_value === 4) {
        sort_list = resList.sort((a: ListHouseRes, b: ListHouseRes) => Number.parseInt(b.house_area) - Number.parseInt(a.house_area))
      } else {
        sort_list = resList
      }
      this.setData({
        resList: sort_list
      })
    }
  },
  showTip() {
    wx.showToast({
      title: '暂不支持其他城市',
      icon: 'none'
    })
  },
  toDetail(e: WechatMiniprogram.TouchEvent) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/houseDetail/houseDetail?id=${id}`
    })
  },
  async addPageList() {
    const res = await getHouseResFilterPage(this.data.page, this.data.house_peace, this.data.house_use, this.data.min_rent, this.data.max_rent, this.data.house_type)
    if (res.statusCode === config.SUCCESS_CODE) {
      if (res.data.data.length === 0) return
      let resList = [
        ...this.data.resList,
        ...res.data.data.filter((item: ListHouseRes) => item.house_status === true)
      ]
      let sort_list: ListHouseRes[]
      if (this.data.sort_value === 1) {
        sort_list = resList.sort((a: ListHouseRes, b: ListHouseRes) => a.house_rent - b.house_rent)
      } else if (this.data.sort_value === 2) {
        sort_list = resList.sort((a: ListHouseRes, b: ListHouseRes) => b.house_rent - a.house_rent)
      } else if (this.data.sort_value === 3) {
        sort_list = resList.sort((a: ListHouseRes, b: ListHouseRes) => Number.parseInt(a.house_area) - Number.parseInt(b.house_area))
      } else if (this.data.sort_value === 4) {
        sort_list = resList.sort((a: ListHouseRes, b: ListHouseRes) => Number.parseInt(b.house_area) - Number.parseInt(a.house_area))
      } else {
        sort_list = resList
      }

      this.setData({
        resList: sort_list
      })
    }
  },
  onLoad() {
    this.initData()
  },
  async onShow() {
    this.setData({
      page: 1,
      resList: []
    })
    this.initList()
    const res = await checkLogin()
    if (res === false && res !== undefined) {
      wx.showToast({ title: '检测到跨设备登录，已退出登录', icon: 'none' })
      removeToken()
      removeUserInfo()
    }
  },
  onReachBottom() {
    this.setData({
      page: this.data.page + 1
    })
    this.addPageList()
  }
})
